package com.taotao.controller;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class FileUtils2 {
    private static List<String> list = new ArrayList<String>();//所有的后缀为.js,.css,.jsp,.html的文件地址
    private static List<String> list1 = new ArrayList<String>();//所有指定后缀的文件地址
    private static HashSet<String> hashSetImages2=new HashSet<>();
    private static HashSet<String> hashSetImages=new HashSet<>();

    public static void main(String[] args) {
        try {
            //String path = "D:\\huiyun\\workspace_git\\easygo-app\\pages\\category";		//要遍历的路径
            String[] strings=new String[]{
                    "pages/index/index",
                    "pages/publish/publish",
                    "pages/publish/choiceCategory/category",
                    "pages/detail/userInfo/userInfo",
                    "pages/detail/detail",
                    "pages/category/category",
                    "pages/message/sessionList/sessionList",
                    "pages/message/messageDetail/messageDetail",
                    "pages/search/search",
                    "pages/userInfo/userInfo",
                    "pages/userInfo/myItemList/myItemList",
                    "pages/map/map",
                    "pages/contactUs/contactUs",
                    "pages/service/service",
                    "pages/showTweets/showTweets",
                    "pages/publish/edit",
                    "pages/message/remind/remind",
                    "pages/userInfo/edit/edit",
                    "pages/address/address",
                    "pages/address/addAndModify/addAndModify",
                    "pages/address/list/list",
                    "pages/order/add/add",
                    "pages/order/list/list",
                    "pages/order/sellList/sellList",
                    "pages/order/info/info",
                    "pages/order/finish/finish",
                    "pages/report/add/add",
                    "pages/test/test",
                    "pages/service/edit",
                    "pages/charge/charge",
                    "pages/charge/record/record",
                    "pages/service/list/list"
        };

            HashSet<String> hashSet=new HashSet<>();
            for (String path:strings) {
                path=path.substring(0,path.lastIndexOf("/"));
//                System.out.println("path->"+path);
                File file = new File("D:/huiyun/workspace_git/easygo-app/"+path);        //获取其file对象
                File[] fs = file.listFiles();    //遍历path下的文件和目录，放在File数组中
                for (File f : fs) {                    //遍历File[]数组
                    if (!f.isDirectory()) {
                        //System.out.println("文件路径->" + f.getAbsolutePath());
                        String absolutePath = f.getAbsolutePath();
                        //String fileName=absolutePath.substring(absolutePath.lastIndexOf("\\")+1,absolutePath.lastIndexOf("."));
                        //String filePath=absolutePath.substring(0,absolutePath.lastIndexOf("\\"));
                        String filePath=absolutePath.substring(0,absolutePath.lastIndexOf("."));
                        hashSet.add(filePath);
                        //System.out.println(filePath);
                        //System.out.println(fileName);
                    }
                }
            }


            File file = new File("D:/huiyun/workspace_git/easygo-app/images/icons");        //images
            File[] fs = file.listFiles();    //遍历path下的文件和目录，放在File数组中
            for (File f : fs) {                    //遍历File[]数组
                if (!f.isDirectory()) {
                    hashSetImages.add(f.getName());
                    System.out.println("images-->"+f.getName());
                }
            }

            for (String filePath:hashSet){
                System.out.println(filePath+" start-----------------");
                FileUtils2.readFile(filePath,hashSetImages);
                System.out.println(filePath+" end-----------------");
            }


            for(String image:hashSetImages){
                if(!hashSetImages2.contains(image)){
                    System.out.println("未使用图片-->"+image);
                }
            }

            
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void readFile(String filePath,HashSet<String> hashSetImages) throws IOException {
        String fileWxml=filePath+".wxml";
        BufferedReader brWxml = new BufferedReader(new FileReader(new File(fileWxml)));
        String lineWxml = null;
        StringBuffer wxmlStringBuffer=new StringBuffer();
        while ((lineWxml = brWxml.readLine()) != null) {
            wxmlStringBuffer.append(lineWxml);
        }
        brWxml.close();
        String wxmlString = wxmlStringBuffer.toString();
        for(String image:hashSetImages){
            if(wxmlString.contains(image)){
                hashSetImages2.add(image);
            }
        }



    }

}